<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
          <script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
     </head>   
<body>
 <span style="color:red" id="msg"></span>
<script type="text/javascript">
//--------------------------------- webSocket ----------------------------------------------
initSocket("user");
  
function initSocket(myWebsocket) {
	var webSocket = null;
	
    window.onbeforeunload = function () {
        //离开页面时的其他操作
    };

    if (!window.WebSocket) {
    	$("#msg").html("您的浏览器不支持websocket！");
        return false;
    }

    var target = 'ws://' + window.location.host + "/activeMQ/websocket/"+myWebsocket;  
	if ('WebSocket' in window) {  
		webSocket = new WebSocket(target);  
	} else if ('MozWebSocket' in window) {  
		webSocket = new MozWebSocket(target);  
	} else {  
	    $("#msg").html('WebSocket is not supported by this browser.');  
	    return;  
	}  
    
    // 收到服务端消息
    webSocket.onmessage = function (msg) {
        $("#msg").append(msg.data+"<br/>");
        // 关闭连接
        webSocket.onclose();
        console.log(msg);
    };

    // 异常
    webSocket.onerror = function (event) {
        console.log(event);
    };

    // 建立连接
    webSocket.onopen = function (event) {
        console.log(event);
    };

    // 断线
    webSocket.onclose = function () {
        console.log("websocket断开连接");
    };
}
</script>
</body>
</html>